package com.esri.core.internal.map;

import com.esri.core.internal.io.handler.n;
import com.esri.core.internal.tasks.e;
import com.esri.core.internal.util.d;
import com.esri.core.io.EsriSecurityException;
import com.esri.core.io.UserCredentials;
import com.esri.core.map.TimeExtent;
import com.esri.core.map.TimeInfo;
import com.esri.core.map.TimeOptions;
import com.esri.core.map.ogc.kml.KmlNode;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.regex.Pattern;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonToken;

/* loaded from: classes.dex */
public class KmlLayerInternal {
    private static final String[] j = {"\\d{4}", "\\d{4}-\\d{2}", "\\d{4}-\\d{2}-\\d{2}", "\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z", "\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}[+|-]\\d{2}", "\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}[+|-]\\d{2}:\\d{2}", "\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}[+|-]\\d{2}:\\d{2}:\\d{2}"};
    private static final String[] k = {"yyyy", "yyyy-MM", "yyyy-MM-dd", "yyyy-MM-dd'T'HH:mm:ss'Z'", "YYYY-MM-DD'T'hh:mm:ss"};
    private SimpleDateFormat a;
    private SpatialReferenceCallback c;
    private KmlNodeEventListener d;
    private String e;
    private TimeExtent f;
    private HashMap<Integer, Date> g;
    private HashMap<Integer, Date> h;
    private UserCredentials i;
    private TimeInfo n;
    private long b = 0;
    private final TimeOptions.Units[] l = {TimeOptions.Units.Years, TimeOptions.Units.Months, TimeOptions.Units.Days, TimeOptions.Units.Seconds, TimeOptions.Units.Seconds};
    private int m = 3;
    private TimeOptions.Units o = TimeOptions.Units.Years;
    private Object p = null;

    /* loaded from: classes.dex */
    public interface KmlNodeEventListener {
        void onNodeRefresh(String str, KmlNode kmlNode);
    }

    /* loaded from: classes.dex */
    public interface SpatialReferenceCallback {
        void updateSpatialReferenceStatus(int i, Object obj);
    }

    static native void nativeSetKMLTempFolder(String str);

    native boolean nativeApplyTimeSeries(long j2, int i, int i2);

    native int nativeBeginTimeInterval(long j2);

    native String nativeBeginTimeValue(long j2, int i);

    native int nativeChildNodeCount(long j2);

    native void nativeClearSelectNodes(long j2);

    native long nativeCreateLayer();

    native int nativeEndTimeInterval(long j2);

    native String nativeEndTimeValue(long j2, int i);

    native String nativeGetChildNode(long j2, long j3);

    native String nativeGetChildNodeFromIndex(long j2, int i);

    native boolean nativeHasTimeSeries(long j2);

    native boolean nativeInitialize(long j2, String str);

    native String nativeSelectNode(long j2, int i);

    native int nativeSelectNodes(long j2, double d, double d2, int i, int i2);

    native void nativeSetNodeVisible(long j2, long j3, boolean z);

    native void nativeSetSelectedNode(long j2, long j3, boolean z);

    native int nativeSetSelectedNodesHighlight(long j2, boolean z);

    native int nativeTimeIntervalCount(long j2);

    native boolean nativeUseTimeSpan(long j2);

    native boolean nativeUseTimeStamp(long j2);

    public KmlLayerInternal(SpatialReferenceCallback spatialReferenceCallback) {
        this.c = spatialReferenceCallback;
        try {
            a(File.createTempFile("temp", ".tmp").getParentFile().getAbsolutePath());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public long a() {
        return this.b;
    }

    public boolean a(String str, UserCredentials userCredentials) {
        this.i = userCredentials;
        long nativeCreateLayer = nativeCreateLayer();
        this.b = nativeCreateLayer;
        return nativeInitialize(nativeCreateLayer, str);
    }

    private static void a(String str) {
        if (new File(str).isDirectory()) {
            nativeSetKMLTempFolder(str);
        }
    }

    public void initMessage(String str) {
        this.e = str;
    }

    public void processEvent(String str, String str2) {
        if (this.d != null) {
            try {
                this.d.onNodeRefresh(str, b(str2));
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }

    public int a(double d, double d2, int i, int i2) {
        long j2 = this.b;
        if (j2 != 0) {
            return nativeSelectNodes(j2, d, d2, i, i2);
        }
        return 0;
    }

    public KmlNode a(int i) throws Exception {
        long j2 = this.b;
        if (j2 != 0) {
            return b(nativeSelectNode(j2, i));
        }
        return null;
    }

    private void b(KmlNode kmlNode, boolean z) {
        if (this.b == 0 || kmlNode == null || kmlNode.getHandle() == 0) {
            return;
        }
        nativeSetNodeVisible(this.b, kmlNode.getHandle(), z);
    }

    private static KmlNode b(String str) throws Exception {
        JsonParser jsonParser;
        try {
            jsonParser = d.c(str);
        } catch (Throwable th) {
            th = th;
            jsonParser = null;
        }
        try {
            jsonParser.nextToken();
            KmlNode fromJson = KmlNode.fromJson(jsonParser);
            if (jsonParser != null) {
                jsonParser.close();
            }
            return fromJson;
        } catch (Throwable th2) {
            th = th2;
            if (jsonParser != null) {
                jsonParser.close();
            }
            throw th;
        }
    }

    public int b() {
        long j2 = this.b;
        if (j2 != 0) {
            return nativeChildNodeCount(j2);
        }
        return 0;
    }

    public KmlNode b(int i) throws Exception {
        long j2 = this.b;
        if (j2 != 0) {
            return b(nativeGetChildNodeFromIndex(j2, i));
        }
        return null;
    }

    public void c() {
        long j2 = this.b;
        if (j2 != 0) {
            nativeClearSelectNodes(j2);
        }
    }

    public int a(boolean z) {
        long j2 = this.b;
        if (j2 != 0) {
            return nativeSetSelectedNodesHighlight(j2, z);
        }
        return 0;
    }

    public List<KmlNode> a(KmlNode kmlNode) throws Exception {
        long j2 = this.b;
        if (j2 != 0) {
            return c(nativeGetChildNode(j2, kmlNode.getHandle()));
        }
        return Collections.emptyList();
    }

    private static List<KmlNode> c(String str) throws Exception {
        JsonParser jsonParser;
        try {
            jsonParser = d.c(str);
        } catch (Throwable th) {
            th = th;
            jsonParser = null;
        }
        try {
            jsonParser.nextToken();
            if (jsonParser.getCurrentToken() == JsonToken.START_ARRAY) {
                ArrayList arrayList = new ArrayList();
                while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
                    KmlNode fromJson = KmlNode.fromJson(jsonParser);
                    if (fromJson != null) {
                        arrayList.add(fromJson);
                    }
                }
                if (jsonParser != null) {
                    jsonParser.close();
                }
                return arrayList;
            }
            if (jsonParser != null) {
                jsonParser.close();
            }
            return Collections.emptyList();
        } catch (Throwable th2) {
            th = th2;
            if (jsonParser != null) {
                jsonParser.close();
            }
            throw th;
        }
    }

    public boolean d() {
        long j2 = this.b;
        if (j2 != 0) {
            return nativeHasTimeSeries(j2);
        }
        return false;
    }

    public TimeExtent e() throws Exception {
        if (this.f == null) {
            this.f = j();
        }
        return this.f;
    }

    private TimeExtent j() throws Exception {
        int g = g();
        String c = c(g);
        String c2 = c((f() + g) - 1);
        if (c == null || c2 == null) {
            return null;
        }
        if (this.a == null) {
            this.a = d(c);
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(this.a.parse(c));
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(this.a.parse(c2));
        return new TimeExtent(calendar, calendar2);
    }

    private void k() {
        Date date;
        boolean l = l();
        if (l) {
            this.h = new HashMap<>();
        }
        for (int g = g(); g < f(); g++) {
            Date date2 = null;
            try {
                date = this.a.parse(c(g));
            } catch (ParseException unused) {
                date = null;
            }
            this.g.put(Integer.valueOf(g), date);
            if (l) {
                try {
                    date2 = this.a.parse(d(g));
                } catch (ParseException unused2) {
                }
                this.h.put(Integer.valueOf(g), date2);
            }
        }
    }

    private boolean l() {
        long j2 = this.b;
        if (j2 != 0) {
            return nativeUseTimeSpan(j2);
        }
        return false;
    }

    private boolean m() {
        long j2 = this.b;
        if (j2 != 0) {
            return nativeUseTimeStamp(j2);
        }
        return false;
    }

    public int f() {
        long j2 = this.b;
        if (j2 != 0) {
            return nativeTimeIntervalCount(j2);
        }
        return 0;
    }

    public int g() {
        long j2 = this.b;
        if (j2 != 0) {
            return nativeBeginTimeInterval(j2);
        }
        return 0;
    }

    public int h() {
        long j2 = this.b;
        if (j2 != 0) {
            return nativeEndTimeInterval(j2);
        }
        return 0;
    }

    public String c(int i) {
        long j2 = this.b;
        if (j2 != 0) {
            return nativeBeginTimeValue(j2, i);
        }
        return null;
    }

    public String d(int i) {
        long j2 = this.b;
        if (j2 != 0) {
            return nativeEndTimeValue(j2, i);
        }
        return null;
    }

    private boolean a(int i, int i2) {
        long j2 = this.b;
        if (j2 == 0 || i2 <= i) {
            return false;
        }
        return nativeApplyTimeSeries(j2, i, i2);
    }

    public boolean a(TimeExtent timeExtent) {
        int i = 0;
        if (this.b != 0 && timeExtent != null) {
            if (this.g == null) {
                this.g = new HashMap<>();
                k();
            }
            Date time = timeExtent.getStartDate() != null ? timeExtent.getStartDate().getTime() : null;
            Date time2 = timeExtent.getEndDate() != null ? timeExtent.getEndDate().getTime() : null;
            if (time != null && time2 != null) {
                Iterator<Integer> it = this.g.keySet().iterator();
                int i2 = 0;
                while (true) {
                    if (!it.hasNext()) {
                        break;
                    }
                    Integer next = it.next();
                    Date date = this.g.get(next);
                    if (i2 == 0 && date.after(time)) {
                        i2 = next.intValue() == 0 ? next.intValue() : next.intValue() - 1;
                    }
                    if (date.after(time2)) {
                        i = next.intValue() == 0 ? next.intValue() : next.intValue() - 1;
                    }
                }
                return nativeApplyTimeSeries(this.b, i2, i);
            }
        }
        return false;
    }

    private SimpleDateFormat d(String str) {
        int i = 0;
        while (true) {
            String[] strArr = j;
            if (i < strArr.length) {
                if (Pattern.compile(strArr[i]).matcher(str).matches()) {
                    if (i >= 4) {
                        this.o = this.l[4];
                        return new SimpleDateFormat(k[4]);
                    }
                    this.o = this.l[i];
                    return new SimpleDateFormat(k[i]);
                }
                i++;
            } else {
                return new SimpleDateFormat(k[0]);
            }
        }
    }

    protected void updateSpatialReferenceStatus(int i) {
        SpatialReferenceCallback spatialReferenceCallback = this.c;
        if (spatialReferenceCallback != null) {
            spatialReferenceCallback.updateSpatialReferenceStatus(this.m, this.p);
        }
    }

    protected boolean fetchUrlData(boolean z, String str, String str2) {
        try {
            return ((Boolean) e.c.submit(new a(z, str, str2)).get()).booleanValue();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e2) {
            if (e2.getMessage() != null && e2.getMessage().contains("EsriSecurityException")) {
                this.p = new EsriSecurityException(-10001, e2.getMessage());
                this.m = -1;
            }
            return false;
        }
    }

    /* loaded from: classes.dex */
    private class a implements Callable<Boolean> {
        private boolean b;
        private String c;
        private String d;

        public a(boolean z, String str, String str2) {
            this.b = z;
            this.c = str;
            this.d = str2;
        }

        @Override // java.util.concurrent.Callable
        /* renamed from: a */
        public Boolean call() throws Exception {
            FileOutputStream fileOutputStream;
            InputStream b;
            InputStream inputStream = null;
            FileOutputStream fileOutputStream2 = null;
            try {
                HashMap hashMap = new HashMap();
                if (this.b && KmlLayerInternal.this.i != null) {
                    String str = this.c;
                    b = com.esri.core.internal.io.handler.a.b(str, hashMap, n.a(str, KmlLayerInternal.this.i));
                } else {
                    b = com.esri.core.internal.io.handler.a.b(this.c, hashMap, (n) null);
                }
                boolean z = false;
                if (b != null) {
                    try {
                        fileOutputStream = new FileOutputStream(new File(this.d));
                    } catch (Throwable th) {
                        fileOutputStream = null;
                        inputStream = b;
                        th = th;
                    }
                    try {
                        byte[] bArr = new byte[4096];
                        while (true) {
                            int read = b.read(bArr);
                            if (read == -1) {
                                break;
                            }
                            fileOutputStream.write(bArr, 0, read);
                        }
                        z = true;
                        fileOutputStream2 = fileOutputStream;
                    } catch (Throwable th2) {
                        InputStream inputStream2 = b;
                        th = th2;
                        inputStream = inputStream2;
                        if (inputStream != null) {
                            inputStream.close();
                        }
                        if (fileOutputStream != null) {
                            fileOutputStream.close();
                        }
                        throw th;
                    }
                }
                if (b != null) {
                    b.close();
                }
                if (fileOutputStream2 != null) {
                    fileOutputStream2.close();
                }
                return Boolean.valueOf(z);
            } catch (Throwable th3) {
                th = th3;
                fileOutputStream = null;
            }
        }
    }

    public void a(KmlNode kmlNode, boolean z) {
        if (this.b == 0 || kmlNode.getHandle() == 0) {
            return;
        }
        nativeSetSelectedNode(this.b, kmlNode.getHandle(), z);
    }

    public void a(KmlNodeEventListener kmlNodeEventListener) {
        this.d = kmlNodeEventListener;
    }

    public TimeInfo i() throws Exception {
        if (this.n == null) {
            if (this.f == null) {
                this.f = j();
            }
            TimeExtent timeExtent = this.f;
            if (timeExtent != null) {
                long timeInMillis = timeExtent.getStartDate().getTimeInMillis();
                long timeInMillis2 = this.f.getEndDate().getTimeInMillis();
                this.n = a(timeInMillis, timeInMillis2, (int) Math.ceil(a((timeInMillis2 - timeInMillis) / f(), this.o)), this.o.getValue());
            }
        }
        return this.n;
    }

    private static double a(double d, TimeOptions.Units units) {
        switch (units) {
            case Seconds:
                return d / 1000.0d;
            case Minutes:
                return (d / 1000.0d) / 60.0d;
            case Hours:
                return ((d / 1000.0d) / 60.0d) / 60.0d;
            case Days:
                return (((d / 1000.0d) / 60.0d) / 60.0d) / 24.0d;
            case Weeks:
                return ((((d / 1000.0d) / 60.0d) / 60.0d) / 24.0d) / 7.0d;
            case Months:
                return ((((d / 1000.0d) / 60.0d) / 60.0d) / 24.0d) / 30.0d;
            case Years:
                return (((((d / 1000.0d) / 60.0d) / 60.0d) / 24.0d) / 30.0d) / 12.0d;
            case Decades:
                return ((((((d / 1000.0d) / 60.0d) / 60.0d) / 24.0d) / 30.0d) / 12.0d) / 10.0d;
            default:
                return d;
        }
    }

    private static TimeInfo a(long j2, long j3, long j4, String str) throws Exception {
        StringBuilder sb = new StringBuilder();
        sb.append("{");
        sb.append("\"startTimeField\" : null,");
        sb.append(" \"endTimeField\" : null,");
        sb.append("\"trackIdField\" : null,");
        sb.append("  \"timeExtent\" : [ " + j2 + ",    " + j3 + " ],");
        sb.append("\"timeReference\" : null,");
        StringBuilder sb2 = new StringBuilder();
        sb2.append("\"timeInterval\" : ");
        sb2.append(j4);
        sb2.append(", ");
        sb.append(sb2.toString());
        sb.append("\"timeIntervalUnits\" : \"" + str + "\", ");
        sb.append("\"exportOptions\" : {\"useTime\" : true, \"timeDataCumulative\" : false, \"timeOffset\" : null, \"timeOffsetUnits\" : null}");
        sb.append("}");
        JsonParser c = d.c(sb.toString());
        c.nextToken();
        TimeInfo fromJson = TimeInfo.fromJson(c);
        c.close();
        return fromJson;
    }
}
